package com.ja.openchat.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;
import com.ja.openchat.ui.me.BlockedUserListActivity;
import com.zy.openchat.core.manager.TgEvent;
import com.zy.openchat.core.model.user.UserInfo;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;


public class PrivacyRightsViewModel extends BaseViewModel {
    public ObservableField<Boolean> isFindByPhoneNumber = new ObservableField<>(UserInfo.getUserInfo().isFindByPhoneNumber);

    public PrivacyRightsViewModel(@NonNull Application application) {
        super(application);
        //同步隐私
//        TelegramManager.getTelegramManager().updateUserPrivacySettingsByAllowFindingByPhoneNumber();
    }

    //点击事件
    public BindingCommand btnOnClickCommand = new BindingCommand(() -> {
        ToastUtils.showShort("点击事件");
    });
    //黑名单点击事件
    public BindingCommand blackNameOnClickCommand = new BindingCommand(() -> {
        startActivity(BlockedUserListActivity.class);
    });

    public BindingCommand phoneSwithOnClickCommand = new BindingCommand(() -> {
        findByPhoneNumberSwitchClick();
    });

    public BindingCommand findByPhoneNumberSwitchOnCheckedChangeCommand = new BindingCommand<Boolean>(new BindingConsumer<Boolean>() {
        @Override
        public void call(Boolean aBoolean) {
        }
    });

    private void resetUI() {
        isFindByPhoneNumber.set(UserInfo.getUserInfo().isFindByPhoneNumber);
    }

    private void findByPhoneNumberSwitchClick() {
        showProgress();
//        TelegramManager.getTelegramManager().setUserPrivacySettingsByAllowFindingByPhoneNumber(getApplication(), !UserInfo.getUserInfo().isFindByPhoneNumber, new TgResultBlock<TgResponse>() {
//            @Override
//            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
//                hideProgress();
//                if (!obj.ok) {
//                    showErrorTip("设置失败，请稍后重试, " + obj.errString);
//                    resetUI();
//                }
//            }
//
//            @Override
//            public void onTimeout(Map<String, Object> request) {
//                hideProgress();
//                showErrorTip("设置失败，请稍后重试");
//                resetUI();
//            }
//        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final TgEvent tg_event) {
        switch (tg_event.event) {
            case EUser_Td_UpdateUserPrivacySettings: {
                resetUI();
            }
            break;
        }
    }
}
